<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <h2>分类列表</h2>
    </div>
    <el-table :data="model" style="width: 100%" stripe border>
      <el-table-column type="index" label="#"></el-table-column>
      <el-table-column prop="_id" label="ID" width="250px"></el-table-column>
      <el-table-column prop="categories" label="所属分类">
        <template slot-scope="scope">
          <el-tag style="margin:0 2px" type="warning" v-for="item in scope.row.categories" :key="item._id">{{item.name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="title" label="标题"></el-table-column>
      <el-table-column prop="date" label="创建时间">
        <template slot-scope="scope">
         <span>{{scope.row.date | formatData}}</span>
        </template>
      </el-table-column>
      <el-table-column width="220px" label="操作">
        <!--通过 Scoped slot 可以获取到 row, column, $index 和 store（table 内部的状态管理）的数据-->
        <template slot-scope="scope">
          <el-button type="warning" size="mini" icon="el-icon-edit"
                     @click="$router.push(`/articles/edit/${scope.row._id}`)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="5"
      layout="total, sizes, prev, pager, next"
      :total="total">
    </el-pagination>
  </el-card>
</template>

<script>
  import dayjs from 'dayjs'
  export default {
    name: "ArticleList",
    data(){
      return{
        model:[],
        currentPage:1,
        pageSize:5,
        total:0
      }
    },
    methods:{
      handleSizeChange(index){
        this.pageSize = parseInt(index)
        this.getArticlesData()
      },
      handleCurrentChange(index){
        this.currentPage = index
        this.getArticlesData()
      },
      async getArticlesData(){
        let sendData = {size:this.pageSize,page:this.currentPage}
        const res = await this.$http.get("/rest/articles",{params:sendData})
        this.model = res.data.list
        this.total = res.data.total
        // console.log(res)
      },
      async remove(row){
        const res = await this.$confirm(`是否要删除标题：${row.title}`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err=>err)
        if(res === 'confirm'){
          await this.$http.delete(`/rest/articles/${row._id}`)
          this.$message({
            message: `成功删除标题：${row.title}`,
            type: 'success'
          });
          this.getArticlesData()
        }else {
          this.$message({
            message: `取消删除操作`,
            type: 'info'
          });
        }
      }
    },
    filters:{
      formatData(value){
        return dayjs(value).format('YYYY-MM-DD HH:mm:ss')
      }
    },
    created() {
      this.getArticlesData()
    }
  }
</script>

<style lang="scss" scoped>

</style>
